Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Vrai-Prix8 * File : src/app/api/municipalites/[slug]/route.ts9 * Module : app10 * Repository : https://git.spboucher.ai/vrai-prix11 * Created : 2026-11-2612 * Last Modified : 2026-11-2613 *14 * Author : Simon-Pierre Boucher15 * Position : Professor of Finance16 * Institution : Université du Québec en Outaouais (UQO)17 * Organization : SP Boucher18 * Website : https://spboucher.ai19 * Git : https://git.spboucher.ai20 *21 * Preferred Contact: contact@spboucher.ai22 * Development : dev@spboucher.ai23 * Support : support@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// GET /api/municipalites/{slug} — portrait d'une municipalité : agrégats du43// rôle (nombre d'unités, valeur totale et médiane 2026, croissance 2021→2026),44// répartition par type, dernières ventes publiées, municipalités voisines et45// échantillon de propriétés. Mêmes données que la page /municipalite/{slug}.46import { NextRequest } from "next/server";47import { munBySlug, munStats, neighborMunicipalities, sampleUnits } from "@/lib/municipalites";48import { EST_YEAR, SITE_URL, TYPE_LABELS, TYPE_SLUGS } from "@/lib/seo";49import { ventesParMunicipalite, vsRolePct } from "@/lib/ventes";50import { cache, entier, erreur, json } from "@/lib/api";5152export const dynamic = "force-dynamic";5354export async function GET(req: NextRequest, { params }: { params: Promise<{ slug: string }> }) {55 const { slug } = await params;56 const q = req.nextUrl.searchParams;57 try {58 const mun = munBySlug(slug.toLowerCase());59 if (!mun) return erreur(404, "municipalité introuvable", "not_found");60 const stats = munStats(mun.nom);61 if (!stats) return erreur(404, "municipalité introuvable", "not_found");62 const ventes = ventesParMunicipalite(mun.nom, entier(q.get("ventes"), 12, 0, 50));63 const echantillon = sampleUnits(mun.nom, null, entier(q.get("echantillon"), 24, 0, 60));64 return json(65 {66 nom: mun.nom,67 slug: mun.slug,68 lat: mun.lat,69 lng: mun.lng,70 millesime: EST_YEAR,71 n: stats.n,72 total2026: stats.total2026,73 total2021: stats.total2021,74 mediane2026: stats.mediane2026,75 croissancePct: stats.croissancePct,76 parType: stats.parType.map((t) => ({77 type: t.type,78 slug: TYPE_SLUGS[t.type] ?? t.type,79 libelle: TYPE_LABELS[t.type]?.pluriel ?? t.type,80 n: t.n,81 total2026: t.total2026,82 mediane2026: t.mediane2026,83 })),84 ventes: {85 n365: ventes.n365,86 median365: ventes.median365,87 rows: ventes.rows.map((r) => ({88 id: r.id,89 date: r.date,90 amount: r.amount,91 street: r.street,92 city: r.city,93 propertyType: r.property_type,94 yearBuilt: r.year_built,95 floorArea: r.floor_area,96 landArea: r.land_area,97 idProvinc: r.id_provinc,98 valeurRole: r.valeur_role,99 vsRolePct: vsRolePct(r.amount, r.valeur_role),100 })),101 },102 voisines: neighborMunicipalities(mun.nom, 10).map((m) => ({ nom: m.nom, slug: m.slug, n: m.n })),103 echantillon: echantillon.map((u) => ({104 id: u.id_provinc,105 adresse: u.adresse,106 apt: u.apt,107 typeProp: u.type_prop,108 est2026: u.est_2026,109 anneeConstruction: u.annee_construction,110 aireEtagesM2: u.aire_etages_m2,111 })),112 links: { page: `${SITE_URL}/municipalite/${mun.slug}` },113 },114 cache(3600)115 );116 } catch (err) {117 return erreur(500, (err as Error).message);118 }119}120